package com.wzt;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.EventListener;

//新建画板类（画板类本身存在，要继承）
public class BirdGame extends JPanel {
    public BufferedImage bg,start,gameover;
    public Ground ground;
    public Bird bird;
    //public Column column;
    public Column[] columns;


    //三种游戏状态
    public int state;
    public static final int START=0;
    public static final int RUNNING=1;
    public static final int GAMEOVER=2;


    public int score=0;//分数


    //无参构造方法给属性赋值
    public BirdGame(){
        try {
            state=START;//强制为初始化状态
            bg= ImageIO.read(getClass().getResource("/img/bg.png"));
            start=ImageIO.read(getClass().getResource("/img/start.png"));
            gameover=ImageIO.read(getClass().getResource("/img/gameover.png"));
            ground=new Ground();//初始化地面属性
            bird=new Bird();
            columns=new Column[2];
            columns[0]=new Column();
            columns[1]=new Column();
            //column=new Column();
        } catch (IOException e) {
            System.out.println("图片加载失败");
        }
    }


    @Override
    public void paint(Graphics g){
        super.paint(g);
        //绘画背景图,顺序不可错
        g.drawImage(bg,0,0,null);//通过Graphics画bg
        switch(state){
            case START:
                g.drawImage(start,0,0,null);
                break;
            case RUNNING:
                //g.drawImage(column.column,column.x,column.y,null);
                g.drawImage(columns[0].column,columns[0].x,columns[0].y,null);
                g.drawImage(columns[1].column,columns[1].x,columns[1].y,null);
                break;
            case GAMEOVER:
                g.drawImage(gameover,0, 0,null);

                break;
        }
        g.drawImage(bird.bird,bird.x,bird.y,null);
        g.drawImage(ground.image,ground.x,ground.y,null);
        setScore(g) ;
        }


    private void setScore(Graphics g) {
        g.setColor(Color.white);
        g.setFont(new Font(Font.SERIF,Font.ITALIC,40));
        g.drawString(score+"",20,60);//画个字符串
    }


    //游戏启动条件, 与动相关
public void action() {

        this.addMouseListener(new MousePressListener());//绑定鼠标监听事件
        this.addKeyListener(new KeyPressListener());

    while (true) {
        switch (state){
            case START:
                ground.step();
                bird.fly();
                break;
            case RUNNING:
                ground.step();
                bird.fly();
                bird.upAndDown();

                if(birdHitGround()){
                    state=GAMEOVER;//鸟撞到了地面
                    break;
                }

                //column.step(); //管道移动
                for(int i=0;i<columns.length;i++){
                    columns[i].step();
                    if(birdHitColumn(columns[i])){
                        state=GAMEOVER;
                        break;
                    }
                    //根据小鸟跟管道的X轴的关系来判断小鸟能不能通过管道
                    if(bird.x==columns[i].x+columns[1].width){
                        score++;
                    }
                }


                break;
            case GAMEOVER:

                break;
        }

        repaint();//再次调用paint方法(每判断一次都要再画一次)

        try {//让移动方法进入休眠来减缓移动速度
            Thread.sleep(1000/60);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

//内部类，声明鼠标监听事件
class MousePressListener extends MouseAdapter{
        int i=0;
    @Override
    public void mousePressed(MouseEvent e) {
        super.mousePressed(e);
        i++;
        System.out.println("点击了"+i+"次");
        switch (state){
            case START:
                state=RUNNING;
                break;
            case RUNNING:
                bird.up();
                break;
            case GAMEOVER:
                state=START;
                bird.x=100;
                bird.y=200;
                bird.speed=0;
                Column.count=0;
                columns[0]=new Column();
                columns[1]=new Column();
                break;
        }
    }
}

class KeyPressListener extends KeyAdapter {
        @Override
        public void keyPressed(KeyEvent e) {
            super.keyPressed(e);
            if(state==RUNNING){
                if(e.getKeyCode()==KeyEvent.VK_SPACE){bird.up();}
            }
        }
    }
//小鸟是否撞击了地面
    public boolean birdHitGround(){
        if(bird.y>=ground.y-bird.height){
            return true;
        }
        return false;
    }

    //鸟撞击管道
    public boolean birdHitColumn(Column column){
        //分析临界状态,分别分析下x,y
        if(bird.x>=column.x-bird.width&&bird.x<=column.x+column.width){
            if(bird.y<=(column.height/2-column.gap/2+column.y)||(bird.y+bird.height)>=(column.height/2+column.gap/2+column.y)){
                return true;
            }else {return false;}
        }
        return false;
    }

}
